<?php
namespace app\admin\model;

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class OnlineApiEs extends BaseModel{
    static private $table_name='online';
    private $times=24;
    public static function index() {
        return 'static_'.self::$table_name.'_21';
    }
    public function Online($server_id=0,$tag='1'){
        $start=11;
        $end=5;
        switch ($tag) {
            case '1440':
                    $end_time= date('Y-m-d 00:00:00');
                    $start_time= date('Y-m-d 00:00:00',strtotime("-{$this->times}days"));
                    $min=1440;//分钟
                    $start=0;
                    $end=10;
                break;
            case '60':
                    $end_time= date('Y-m-d H:00:00');
                    $start_time= date('Y-m-d H:00:00',strtotime("-{$this->times}hours"));
                    $min=60;//分钟
                break;
            case '5':
                    $end_time= date('Y-m-d H:i:00');
                    $start_time= date('Y-m-d H:i:00',strtotime('-'.($this->times*5).'minutes'));
                    $min=5;//分钟
                break;
            default:
                    $end_time= date('Y-m-d H:i:00');
                    $start_time= date('Y-m-d H:i:00',strtotime("-{$this->times}minutes"));
                    $min=1;//分钟
                break;
        }
        $tag.='m';
        if(strtotime($end_time)<= strtotime($start_time)){
            $this->index("时间格式错误，结束时间小于开始时间");
        }
        $body=[
                "size"=>0,
                "query"=>[
                  "bool"=>[
                    "must"=>[
                      [
                        "range"=>[
                          "optime"=>[
                            "gte"=>"{$start_time} +08:00",
                            "lte"=>"{$end_time} +08:00"
                          ]
                        ]
                      ]
                    ]
                  ]
                ],"aggs"=>[
                    "group_by_time"=>[
                      "date_histogram"=>[
                        "field"=>"optime",
                        "interval"=>"{$tag}"
                      ],"aggs"=>[
                        "group_by_server"=>[
                          "terms"=>[
                            "field"=>"sid"
                           ],"aggs"=>[
                                "max_sid"=>[
                                  "max"=>[
                                    "field"=>"num"
                                   ]
                                ]
                        ]
                      ]
                    ]
                  ]
                ]
              ];
        if($server_id){
            $body['query']['bool']['must'][]=['match'=>['sid'=>$server_id]];
        }
        $online=$this->setIndex(self::index())->SetBody($body)->search();
        $deal_arr=[];
        foreach ($online['group_by_time']['buckets'] as $one){
            $key=substr($one['key_as_string'], 2,14);
            $sum_online=0;
            foreach ($one['group_by_server']['buckets'] as $one_max){
                $sum_online+=$one_max['max_sid']['value'];
            }
            $deal_arr[$key]=['date'=>$key,'online_num'=>$sum_online];
        }
        for ($i=0;$i<$this->times;$i++){
            $key_tag= substr(date('Y-m-d H:i:s',strtotime($end_time)-60*$min*$i), 2,14);
            if(isset($deal_arr[$key_tag])){
                $time_arr[]=['date'=>$key_tag,'online_num'=>$deal_arr[$key_tag]['online_num']];
            }else{
                $time_arr[]=['date'=>$key_tag,'online_num'=>0];
            }
        }
        $time_arr=array_reverse($time_arr);
        return $time_arr;
    }
}

